<template>
	<view>
		<cu-custom bgColor="bg-gradual-pink" :isBack="true"><block slot="backText">返回</block>
			<block slot="content">赛事管理</block>
		</cu-custom>
		<scroll-view scroll-x class="bg-white nav text-center" :style="[{top:CustomBar + 'px'}]">
			<view class="cu-item csitem" :class="index==TabCur?'text-blue cur':''" v-for="(item,index) in tabNav" :key="index" @tap="tabSelect"
			 :data-id="index">
				{{tabNav[index]}}
			</view>
		</scroll-view>
		<block v-if="TabCur==0">
			<view>1</view>
		</block>
		<block v-if="TabCur==1">
			<view class="changestate bg-white">
				<view class="cu-form-group">
					<view class="title">赛事状态选择</view>
					<radio-group @change="state_sel">
						<label style="margin-right: 30rpx;" ><radio value="0" :checked="state==0?true:false" />比赛改期</label>
						<label><radio value="-1" :checked="state==-1?true:false" />比赛取消</label>
					</radio-group>
				</view>
				<view v-if="state==0">
					<view class="cu-form-group">
						<view class="title">报名截止时间</view>
						<picker mode="date" :value="joinedate"  @change="joineDateChange">
							<view class="picker">
								{{joinedate}}
							</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title">比赛开始时间</view>
						<picker mode="date" :value="sdate"  @change="sDateChange">
							<view class="picker">
								{{sdate}}
							</view>
						</picker>
					</view>
					<view class="cu-form-group">
						<view class="title">比赛截止时间</view>
						<picker mode="date" :value="edate"  @change="eDateChange">
							<view class="picker">
								{{edate}}
							</view>
						</picker>
					</view>
				</view>
				
				
				
				<view class="title bg-white reason-title">停改期原因</view>
				<view class="state_reason bg-white">
					<textarea v-model="reason"></textarea>
				</view>
				<view class="state_btn" >
					<button class="state_save bg-blue" @click="statesave">保存并提交</button>
				</view>
			</view>
			
		</block>
		
	</view>
</template>

<script>
	export default {
		onLoad(option){
			this.mid=option.mid;
			this.userid=uni.getStorageSync("uid");
			this.openid=uni.getStorageSync('openid');
			console.log(this.openid);
			if(this.userid.length<=0 || this.openid.length<=0){
				uni.showModal({
				    title: '提示',
				    content: '您暂未登陆,是否马上登陆',
				    success: function (res) {
				        if (res.confirm) {
							uni.switchTab({
								url:"/pages/tennis/user/index"
							})
				        } else if (res.cancel) {
				           uni.navigateBack({
				           	
				           })
				        }
				    }
				});
				return;
			}
			
			uni.showLoading({
				title:'加载中..'
			})
			uni.request({
				url:"https://www.52tennis.cn/Match/GetMatchInfo",
				data: {
					matchid:option.mid,
					userid:this.userid
				},
				success:(data)=> {
					console.log(data);
					if(data.data.result==1){
						console.log(data.data.returnjson);
						this.match=data.data.returnjson;
						if(this.match.iscreate==false){
							uni.showModal({
							    title: '错误提示',
							    content: '您暂无权限管理此赛事',
							    success: function (res) {
							        if (res.confirm) {
										uni.navigateBack({
										})
							        } else if (res.cancel) {
							           uni.navigateBack({
							           })
							        }
							    }
							});
							
						}
						this.state=this.match.state;
						if(this.state==4){
							uni.showModal({
							    title: '提示',
							    content: '此赛事已结束,无法更改状态',
							    success: function (res) {
							        if (res.confirm) {
										uni.navigateBack({
										})
							        } else if (res.cancel) {
							           uni.navigateBack({
							           })
							        }
							    }
							});
						}
						this.reason=this.match.updatereason;
						this.sdate=this.match.startime;
						this.edate=this.match.endtime;
						this.joinedate=this.match.joinendtime;
					}else{
						uni.showModal({
						    title: '错误提示',
						    content: data.data.msg,
						    success: function (res) {
						        if (res.confirm) {
						        } else if (res.cancel) {
						          
						        }
						    }
						});
					}
				},
				complete: () => {
					uni.hideLoading();
				}
			});
			
			
		},
		data() {
			return {
				mid:0,
				userid:"",
				openid:"",
				tabNav:['报名管理','赛事变更管理'],
				TabCur: 0,
				CustomBar: this.CustomBar,
				modalName: null,
				reason:'',
				state:'',
				match:{},
				sdate:'',
				edate:'',
				joinedate:''
			}
		},
		methods: {
			tabSelect(e){
				this.TabCur=e.currentTarget.dataset.id;
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			},
			state_sel(e){
				this.state=e.detail.value;
			},
			statesave(e){
				let _this=this;
				uni.showModal({
				    title: '确认提示',
				    content: '确认修改赛事状态吗?',
				    success: function (res) {
				        if (res.confirm) {
							if(_this.state!=0 && _this.state!=-1){
								uni.showModal({
								    title: '提示',
								    content: "请选择赛事状态后再保存",
								    success: function (res) {
								        if (res.confirm) {
								        } else if (res.cancel) {
								        }
								    }
								});
								return;
							}
							if(new Date(_this.joinedate).getTime()-new Date().getTime()<0){
								uni.showModal({
								    title: '修改失败',
								    content: "报名截至日期大于今天",
								    success: function (res) {
								        if (res.confirm) {
								        } else if (res.cancel) {
								        }
								    }
								});
								return;
							}
							
							if(_this.state==0){
								if(new Date(_this.edate).getTime()-new Date(_this.sdate).getTime()<0){
									uni.showModal({
									    title: '修改失败',
									    content: "比赛结束日期应当大于等于比赛结束日期",
									    success: function (res) {
									        if (res.confirm) {
									        } else if (res.cancel) {
									        }
									    }
									});
									return;
								}
								
								if(new Date(_this.sdate).getTime()-new Date(_this.joinedate).getTime()<0){
									uni.showModal({
									    title: '修改失败',
									    content: "报名截至日期应当小于比赛开始日期",
									    success: function (res) {
									        if (res.confirm) {
									        } else if (res.cancel) {
									        }
									    }
									});
									return;
								}
								
							}
							
							//updateMatchState
							uni.request({
								url:"https://www.52tennis.cn/Match/updateMatchState",
								header:{
									'Content-Type': 'application/json;charset=UTF-8' //自定义请求头信息
								},
								method:'POST',
								data: {
									'matchid':_this.mid,
									'state':_this.state,
									'reason':_this.reason,
									'sdate':_this.sdate+" 00:00:00",
									'edate':_this.edate+" 23:59:59",
									'joinedate':_this.joinedate+" 23:59:59"
								},success:(data)=> {
									console.log(data);
									if(data.data.result==1){
										uni.showToast({
											title: '修改成功',
											icon: 'success',
											mask: true,
											duration: 3000
										})
										setTimeout(function(){
											uni.navigateBack({
											})
										},2000)
										
									}else{
										uni.showModal({
										    title: '修改失败',
										    content: data.data.msg,
										    success: function (res) {
										        if (res.confirm) {
										          uni.navigateBack({
										          })
										        } else if (res.cancel) {
										           
										        }
										    }
										});
									}
								}
									
							})
				        } else if (res.cancel) {
				          
				        }
				    }
				});
			},
			sDateChange(e){
				this.sdate=e.detail.value;
			},
			eDateChange(e){
				this.edate=e.detail.value;
			},
			joineDateChange(e){
				this.joinedate=e.detail.value;
			}
		}
	}
</script>

<style>
	.changestate{
		padding:0rpx 30rpx;
		border-top: 1rpx solid #bfbcbe;
		
	}
	.changestate .cu-form-group{
		padding:0rpx;
	}
	.changestate radio{
		transform: scale(0.7);
		
	}
	.reason-title{
		border-top: 1rpx solid #bfbcbe;
		padding:20rpx 0rpx;
	}
	
	.state_reason textarea{
		border: 1px solid #bfbcbe;
		padding:15rpx;
		border-radius: 15rpx;
		width:100%;
		height: 180rpx;
		overflow: auto;
		word-break: break-all;
	}
	.state_btn{
		margin-top: 90rpx;
	}
	.state_save{
		height: 80rpx;
		line-height: 80rpx;
		font-size: 32rpx;
	}
</style>
